<html>
<head><meta charset="utf-8"><title>Find whether resolving within a trait or function? · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Find.20whether.20resolving.20within.20a.20trait.20or.20function.3F.html">Find whether resolving within a trait or function?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="201921451"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Find%20whether%20resolving%20within%20a%20trait%20or%20function%3F/near/201921451" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Find.20whether.20resolving.20within.20a.20trait.20or.20function.3F.html#201921451">(Jun 25 2020 at 00:48)</a>:</h4>
<p>I'm trying to find the nearest scope that's either a function item or a trait impl: <a href="https://github.com/rust-lang/rust/pull/73566#discussion_r444631273">https://github.com/rust-lang/rust/pull/73566#discussion_r444631273</a><br>
but the RibKind keeps coming up as AssocItemRibKind even if it's nested inside a function:</p>
<div class="codehilite"><pre><span></span><code><span class="k">struct</span> <span class="nc">ValidType</span><span class="p">;</span><span class="w"></span>
<span class="k">trait</span><span class="w"> </span><span class="n">NeedsBody</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">f</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">outer</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">impl</span><span class="w"> </span><span class="n">NeedsBody</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">ValidType</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">fn</span> <span class="nf">f</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">content</span>::<span class="n">shouldnt</span>::<span class="n">matter</span><span class="p">();</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<div class="codehilite"><pre><span></span><code>$ RUSTDOC_LOG=rustc_resolve rustdoc +stage1 nested-impl.rs
[DEBUG rustc_resolve::late] should_report_errs(source=AssocItemRibKind)
error[E0433]: failed to resolve: use of undeclared type or module `content`
</code></pre></div>


<p>this is my code:</p>
<div class="codehilite"><pre><span></span><code><span class="w">        </span><span class="c1">// Find the first rib which is either a function or item.</span>
<span class="w">        </span><span class="c1">// If we don&#39;t find one, we&#39;re not in an item body and so should report an error.</span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">ribs</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">ribs</span><span class="p">.</span><span class="n">value_ns</span><span class="p">.</span><span class="n">iter</span><span class="p">().</span><span class="n">rev</span><span class="p">();</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">kind</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">loop</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="k">match</span><span class="w"> </span><span class="n">ribs</span><span class="p">.</span><span class="n">next</span><span class="p">().</span><span class="n">map</span><span class="p">(</span><span class="o">|</span><span class="n">r</span><span class="o">|</span><span class="w"> </span><span class="n">r</span><span class="p">.</span><span class="n">kind</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="nb">None</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span><span class="w"></span>
<span class="w">                </span><span class="nb">Some</span><span class="p">(</span><span class="n">rib</span><span class="w"> </span><span class="o">@</span><span class="w"> </span><span class="p">(</span><span class="n">RibKind</span>::<span class="n">FnItemRibKind</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">ItemRibKind</span><span class="p">(</span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">AssocItemRibKind</span><span class="p">))</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="k">break</span><span class="w"> </span><span class="n">rib</span><span class="p">,</span><span class="w"></span>
<span class="w">                </span><span class="n">_</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="k">continue</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="p">};</span><span class="w"></span>
</code></pre></div>


<p>What am I doing wrong?</p>



<a name="201921466"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Find%20whether%20resolving%20within%20a%20trait%20or%20function%3F/near/201921466" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Find.20whether.20resolving.20within.20a.20trait.20or.20function.3F.html#201921466">(Jun 25 2020 at 00:48)</a>:</h4>
<p>(cross-posted from <a href="https://discordapp.com/channels/442252698964721669/443151243398086667/725511142360875029">https://discordapp.com/channels/442252698964721669/443151243398086667/725511142360875029</a>)</p>



<a name="201921524"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Find%20whether%20resolving%20within%20a%20trait%20or%20function%3F/near/201921524" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Find.20whether.20resolving.20within.20a.20trait.20or.20function.3F.html#201921524">(Jun 25 2020 at 00:49)</a>:</h4>
<p>oops, this should have been in <code>t-compiler/help</code>, anyone know how to move it?</p>



<a name="201921844"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Find%20whether%20resolving%20within%20a%20trait%20or%20function%3F/near/201921844" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Find.20whether.20resolving.20within.20a.20trait.20or.20function.3F.html#201921844">(Jun 25 2020 at 00:54)</a>:</h4>
<p>oh hmm I think the function itself counts as an associated item</p>



<a name="201921848"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Find%20whether%20resolving%20within%20a%20trait%20or%20function%3F/near/201921848" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Find.20whether.20resolving.20within.20a.20trait.20or.20function.3F.html#201921848">(Jun 25 2020 at 00:54)</a>:</h4>
<p>well that makes this hard then</p>



<a name="201922691"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Find%20whether%20resolving%20within%20a%20trait%20or%20function%3F/near/201922691" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Find.20whether.20resolving.20within.20a.20trait.20or.20function.3F.html#201922691">(Jun 25 2020 at 01:10)</a>:</h4>
<p>Might have gotten it: <a href="https://github.com/rust-lang/rust/pull/73566/commits/a17dd2de6640fb3f0b8a7485f15be5b7cfee6463">https://github.com/rust-lang/rust/pull/73566/commits/a17dd2de6640fb3f0b8a7485f15be5b7cfee6463</a></p>



<a name="201927943"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Find%20whether%20resolving%20within%20a%20trait%20or%20function%3F/near/201927943" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Find.20whether.20resolving.20within.20a.20trait.20or.20function.3F.html#201927943">(Jun 25 2020 at 03:04)</a>:</h4>
<p>giant thank you to whoever wrote <a href="https://rustc-dev-guide.rust-lang.org/name-resolution.html?highlight=rib#scopes-and-ribs">https://rustc-dev-guide.rust-lang.org/name-resolution.html?highlight=rib#scopes-and-ribs</a> btw, that was <em>super</em> helpful</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>